<template>
  <div class="login">
    <h2>登录</h2>
    <nut-form ref="ruleForm" :model-value="formData">
      <nut-form-item required prop="name" :rules="[{ required: true, message: '请输入用户名' }]">
        <input v-model="formData.name" class="nut-input-text" placeholder="请输入用户名" type="text" />
      </nut-form-item>
      <nut-form-item required prop="pwd" :rules="[{ required: true, message: '请填写联系电话' }]">
        <input v-model="formData.pwd" class="nut-input-text" placeholder="请输入密码" type="password" />
      </nut-form-item>
      <nut-button block type="info" @click="login()"> 登录 </nut-button>
    </nut-form>
  </div>
</template>

<script   name="LoginPage">
  import {getUserInfo} from '/src/api/index'
  import { useRouter } from 'vue-router';
  const router = useRouter();
  import { useUserStore } from "/src/store/modules/user";
  import Toast from 'vant/lib/toast';
  import showToast from 'vant'
  import 'vant/es/toast/style';
  import { Notify } from 'vant';
  import 'vant/es/notify/style';

  export default {
    name: '',
    setup() {
      const state = reactive({
        tab1value: '0',
      });
      return { state };
    },
    data() {
      return {
        // 测试
        userStore : useUserStore(),
        router: useRouter(),
        formData : {
          name: '',
          pwd: '',
        }
      };
    },
    async created(){
      Notify({
        message: '自动登录中...',
        type: 'primary',
        background: '#1989fa',
        duration: 600,
        // color: '#ad0000',
        // background: '#ffe1e1',
      });
      // Toast.loading({message:"自动登录中",duration: 600})


      var url = window.location.href;             //获取当前url

      // var dz_url = url.split('#')[0];                //获取#/之前的字符串
      // var dz_url = url.split('/')[0];                //url的字符串

      //找到id，测试时候注释
      var cs = url.split('?')[1];                //获取?之后的参数字符串
      console.log('cs1:',cs)
      var cs_arr = cs.split('&');                    //参数字符串分割为数组
      var cs={};
      for(var i=0;i<cs_arr.length;i++){         //遍历数组，拿到json对象
        cs[cs_arr[i].split('=')[0]] = cs_arr[i].split('=')[1]
      }
      console.log('cs1:',cs)

      /*//自定义id
      let cs = {}
      cs.id = '202114137';*/


      // 查询信息并转json
      let userStore = useUserStore()
      userStore.setUserLoginToken(cs.account)
      const res = await getUserInfo(cs.account);
      if(200 != res.response.value.code){
        // Toast.fail("自动登录失败")
        Notify({
          background: '#ee0a24',
          message: '自动登录失败' });
      }
      // console.log('getUserInfo',res)
      // console.log('getUserInfo',res.response)
      // console.log('getUserInfo',res.response.value)
      let info = res.response.value.extend
      //console.log('info',info)
      // console.log('userStore.getUserToken()',userStore.getUserToken)
      // console.log(info)
      // let userInfo = info.USER.replace(/\\/g,'')
      // let userJson = JSON.parse(userInfo)
      //存储
      // console.log('info.TYPE',info.TYPE)
      // userStore.setUserType(info.TYPE)
      // console.log(userStore.getUserInfo)
      // console.log(userStore.getUserType)
      // 根据返回结果跳转页面
      // userStore.setInfo(info,'stud')
      // this.userStore.setUserType(0)
      // this.router.push('/home');
      userStore.setUserType(info.TYPE)
      if(info.TYPE == 'stud'){
        userStore.setInfo(info.userInfo,info.TYPE)
        localStorage.setItem("userType", info.TYPE)
        //console.log(info.userInfo)
        // this.userStore.setUserType(0)
        this.router.push('/mainMenu');
      }else if(info.TYPE == 'tea'){
        // this.userSt
        // ore.setUserType(1)
        localStorage.setItem("userType", info.TYPE)
        console.log('teacher', info);
        userStore.setInfo(info.userInfo,info.TYPE)
        this.router.push('/mainMenu_tea');
      }else {
        //身份信息错误
        alert('身份信息错误')
      }
    },
    methods: {
      async login(){
        // const res = await getUserInfo('202114137');
        // console.log(res.response.value.extend.userInfo)
        let USER = res.response.value.extend.userInfo.USER

        let json = USER.replace(/\\/g,"")
        // console.log(JSON.parse(json))
        this.router.push('/home');
      },
    },
  };
</script>

<style scoped lang="scss">
  .login {
    padding: 20px;
    h2 {
      text-align: center;
      letter-spacing: 10px;
    }
    .nut-form-item {
      background: #f2f3f5;
      border-radius: 20px;
      margin-bottom: 20px;

      input {
        background: transparent;
      }
    }
  }
</style>
